<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="lib/admin/admin.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="entry">入库</a>
    <!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
<fieldset class="layui-elem-field layui-field-title">
    <legend>货物入库管理</legend>
</fieldset>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">货验收单</label>
                        <div class="layui-input-inline" style="margin-right: 10px">
                            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入您想搜索的货验收单" id="deliveryOrderCode" class="layui-input" style="width: 250px">
                        </div>
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search" onclick="selectDO()">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>

                    <!--    <div class="layui-inline">-->
                    <!--        <label class="layui-form-label" style="width: 150px">显示未验收的商品</label>-->
                    <!--        <div class="layui-input-inline">-->
                    <!--            <input type="checkbox" name="close" lay-skin="switch" checked="" lay-text="ON|OFF">-->
                    <!--        </div>-->
                    <!--    </div>-->

                <div class="layui-card-body">
                    <button type="button" class="layui-btn" onclick="entry()">货物入库</button>
                    <table class="layui-hide" id="test" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>


<!--<button type="button" class="layui-btn"  onclick="insertDelivery()">批量收货</button>-->
<!--<table class="layui-hide" id="test1" lay-filter="test"></table>-->



<table>

</table>
<script src="lib/jquery-1.12.4.js"></script>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->


<script>
    layui.use('table', function(){
        var table = layui.table;
        //监听行工具事件
        table.render({
            elem: '#test'
            ,url:'pk/deliveryOrder/listDeliveryOrder'
            ,where :{
                status : 3
            }
            ,parseData: function(res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": 1, //解析数据长度
                    "data": res.obj.list //解析数据列表
                };
            }
            ,page:true
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,cols: [[
                {type:'checkbox'}
                ,{field:'deliveryOrderCode', title: '货验收单号', sort: true}
                ,{field:'purchaseOrderCode',title: '上游单号', sort: true}
                ,{field:'supplierName', title: '供应商名称', sort: true}
                ,{field:'statusName', title: '状态', sort: true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'edit'){
                window.deliveryOrderId = data.id;
                showDelivery();

            } else if(obj.event === 'entry'){
                console.log(obj.data);

                layer.confirm('确认入库?', { btn: ['确认','取消'],btn1: function(){
                        var data1 = [];
                        data1.push({"deliveryOrderId":obj.data.id});
                        // var data = JSON.stringify(table.cache.test);
                        // console.log(data.length); 显示字符长度,不是数组长度
                        console.log(data1);
                        var data = JSON.stringify(data1);

                        var staffId = 2;

                        $.ajax({
                            url: 'pk/warehouseEntry/insertWhEntryR?staffId='+staffId,
                            type: "POST",
                            data: data,
                            contentType:"application/json;charset=UTF-8",
                            success:function () {
                                layer.msg("入库完成");
                                selectDO();
                            }
                        });
                    },
                    btn2: function(){

                    }
                });
            }
        });
    });

    function selectDO() {
        layui.use('table', function () {
            var deliveryOrderCode = $("#deliveryOrderCode").val();
            var table = layui.table;
            table.reload('test', {
                page: {
                    curr: 1
                }, where: {
                    deliveryOrderCode: deliveryOrderCode
                }
            })
        });
    }

    function showDelivery() {
        var layer = layui.layer;
        layer.open({
            type: 2,
            area: ['600px','380px'],
            content: 'delivery_inspection_detail2.html' //这里content是一个普通的String
        });
    }

    function entry() {
        var table = layui.table;
        var checkStatus = table.checkStatus('test');
        var data2 = checkStatus.data;//获取的所有
        if(data2.length<1){
            layer.alert("请选择至少一条货验收单");
            // layer.alert(data[0].id);
        }else {
            var data1 = [];
            layer.confirm('确认生成入库单?', { btn: ['确认','取消'],btn1: function(){
                    // var data = JSON.stringify(table.cache.test);
                    // console.log(data.length); 显示字符长度,不是数组长度
                    for (var i=0;i<data2.length;i++) {
                        data1.push({"deliveryOrderId":data2[i].id});
                    }
                    var data = JSON.stringify(data1);
                    var staffId = 2;
                    console.log(data);

                    $.ajax({
                        url: "pk/warehouseEntry/insertWhEntryR?staffId="+staffId,
                        type: "POST",
                        data: data,
                        traditional: true
                        ,contentType:"application/json;charset=UTF-8",
                        success:function () {
                            layer.msg("入库完成");
                            selectDO();
                        }
                    });
                },
                btn2: function(){

                }
            });
        }
    }

</script>
</body>
</html>